package com.fenghuajueli.idiomppp.utils;

import android.content.Context;

import com.blankj.utilcode.util.LogUtils;
import com.fenghuajueli.libbasecoreui.utils.MmkvUtils;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class DBUtils {

    public static void copyDbFile(Context context, String db_name) {
        InputStream in = null;
        FileOutputStream out = null;
        File filePath = context.getDatabasePath(db_name);
        //spUtils 是为了防止多次拷贝
        if (!MmkvUtils.mmkv.decodeBool("isCopy", false)) {
            try {
                in = context.getAssets().open(db_name); // 从assets目录下复制
                out = new FileOutputStream(filePath);
                int length = -1;
                byte[] buf = new byte[1024];
                while ((length = in.read(buf)) != -1) {
                    out.write(buf, 0, length);
                }
                out.flush();
                MmkvUtils.mmkv.encode("isCopy", true);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (in != null) in.close();
                    if (out != null) out.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            LogUtils.d("数据库复制成功---------");
        } else {
            LogUtils.d("数据库已经复制过---------");
        }
    }


}
